package com.todo.event.activity.application.handler.fieldtransfer;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.exception.BizException;
import com.todo.event.activity.application.entity.context.FieldTransferContext;
import com.todo.event.activity.common.enums.EventFieldDataSourceEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @author azhebuxing
 * @date 2025/1/9 19:27
 * @description
 */
@Slf4j
public abstract class AbstractFieldTransferHandle {

    public String fieldTransfer(FieldTransferContext context) {
        if (context == null) {
            return null;
        }
        if (!doMatch(context)) {
            log.warn("AbstractFieldTransferHandle#FieldTransfer  匹配失败 处理器：【{}】 context:【{}】", getHandleType(), JSONObject.toJSONString(context));
            return null;
        }
        try {
            String fieldValue = doTransfer(context);
            return fieldValue;
        } catch (BizException ex) {
            log.error("AbstractFieldTransferHandle#FieldTransfer 业务异常 处理器：【{}】 context:【{}】", getHandleType(), JSONObject.toJSONString(context), ex);
        } catch (Exception ex) {
            log.error("AbstractFieldTransferHandle#FieldTransfer 处理器：【{}】 系统异常 context:【{}】", getHandleType(), JSONObject.toJSONString(context), ex);
        }
        return null;
    }

    /**
     * 实际转化操作
     *
     * @param context
     * @return
     */
    protected abstract String doTransfer(FieldTransferContext context);


    /**
     * 转化匹配验证
     *
     * @param context
     * @return
     */
    protected abstract Boolean doMatch(FieldTransferContext context);

    protected abstract EventFieldDataSourceEnum getHandleType();
}
